﻿// -----------------------------------------------------------------------
// <copyright file="AltaMateriaAction.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------


namespace TPTecnicas.MessageProcessor.Actions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using TPTecnicas.MessageProcessor.Common;
    using System.Text.RegularExpressions;
    using TPTecnicas.MessageProcessor.Business;
    using System.ComponentModel.Composition;


    public class AltaMateria : Action
    {
        [ImportingConstructor]
        public AltaMateria([Import("context")]IUniversityContext context)
            : base(context)
        {
        }

        public override void Execute(IMessage message)
        {
            var regex = new Regex(@"^\[ALTA-MATERIA-(?<codigo>\w+)\] (?<padron>\d+)-(?<nombre>.*)$");
            var match = regex.Match(message.Subject);
            var codigo = match.Groups["codigo"].Value;
            var padron = match.Groups["padron"].Value;
            var nombre = match.Groups["nombre"].Value;

            var student = Context.StudentRepository.First(s => s.Padron.Equals(padron));
            var subject = Context.SubjectRepository.First(s => s.Code.Equals(codigo));

            Context.StudentRepository.SubscribirAMateria(student, subject);
        }
    }
}
